import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:hixing_app/const/iphone_config.dart';
import 'package:hixing_app/utils/logs.dart';

class DeviceUtils {
  static DevHardwareInfo deviceInfo = DevHardwareInfo();

  /// 设备硬件信息
  static Future<DevHardwareInfo> getDevHardWare() async {
    final DeviceInfoPlugin _info = DeviceInfoPlugin();
    if (Platform.isAndroid) {
      final AndroidDeviceInfo androidInfo = await _info.androidInfo;
      deviceInfo = DevHardwareInfo(
        deviceId: '${androidInfo.id}',
        deviceName: (androidInfo.isPhysicalDevice ?? true)
            ? '${androidInfo.model}'
            : 'Andorid-[Simulator]',
        platform: 'android',
        deviceVersion: '${androidInfo.version.release}',
      );
      logs('Dev info Android : ${deviceInfo.toString()}');
      return deviceInfo;
    }
    if (Platform.isIOS) {
      final iosInfo = await _info.iosInfo;
      deviceInfo = DevHardwareInfo(
        deviceId: '${iosInfo.identifierForVendor}',
        deviceName: iosInfo.isPhysicalDevice
            ? IPhoneConfig.phoneModel('${iosInfo.utsname.machine}')
            : '${iosInfo.utsname.machine}-[Simulator]',
        platform: 'ios',
        deviceVersion: '${iosInfo.systemVersion}',
      );
      logs('Dev info IOS : ${deviceInfo.toString()}');
      return deviceInfo;
    }
    return DevHardwareInfo(
      deviceId: '-1',
      deviceName: 'UnKnown',
      platform: Platform.operatingSystem,
      deviceVersion: 'UnKnown',
    );
  }

// 判断用户设备是否为64位
  static Future<bool> is64Bit() async {
    if (Platform.isAndroid) {
      final DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
      final AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
      return androidInfo.supported64BitAbis.isNotEmpty;
    } else if (Platform.isIOS) {
      return true;
    }

    return false;
  }
}

/// 设备硬件信息
class DevHardwareInfo {
  String deviceId = '';
  String deviceName = '';
  String platform = '';
  String deviceVersion = '';

  DevHardwareInfo({
    this.deviceId = '',
    this.deviceName = '',
    this.platform = '',
    this.deviceVersion = '',
  }) : super();

  @override
  String toString() {
    return 'deviceId: $deviceId , deviceName: $deviceName , platform: $platform, deviceVersion: $deviceVersion';
  }
}
